//
//  WKWebViewController.m
//  BaichuanDemo
//
//  Created by a on 2019/3/20.
//  Copyright © 2019 MGXD. All rights reserved.
//

#import "WKWebViewController.h"

@interface WKWebViewController () <WKNavigationDelegate>
@end

@implementation WKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webview = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webview];
    self.webview.navigationDelegate = self;
//    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://s.click.taobao.com/iHin4Ew"]]];//单品
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://pub.alimama.com/promo/item/channel/index.htm?spm=a219t.7900221/1.1998910419.d07bd19c4.2a8f75a5aFHCHs&channel=nzjh"]]];//女装尖货

}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//    [SVProgressHUD dismiss];
//    _urls = webView.URL.absoluteString;
//    NSLog(@"加载完成后调用：goods_id=%@ url=%@",[PublicClass getGoodsIdWithUrl:webView.URL.absoluteString],webView.URL.absoluteString);
//    [self setBuyButtonStatus:webView];
    
    //通过js获取网页图片
    //    [self getImageUrlByJS:webView];
    
    NSString *doc = @"document.body.outerHTML";
    [webView evaluateJavaScript:doc completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
        if (error) {
            NSLog(@"JSError:%@",error);
        }
        
        NSLog(@"网页源码html====%@",htmlStr);
        
        //        NSString *str = [htmlStr]
//        NSArray *array = [self filterImage:htmlStr]; //获取webView中的所有图片URL
//        for (NSArray *string in array) {
//            NSLog(@"%@",string);
//        }
//
//        return ;
//        ImageList *list = [[ImageList alloc] init];
//        list.dataArray = array;
//        [self.navigationController pushViewController:list animated:YES];
    }] ;
}



-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSURLComponents *components = [[NSURLComponents alloc] initWithString:navigationAction.request.URL.absoluteString];
    if (![[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] 'https' OR SELF MATCHES[cd] 'http' OR SELF MATCHES[cd] 'file' OR SELF MATCHES[cd] 'about'"] evaluateWithObject:components.scheme]) {
//        [SVProgressHUD dismiss];//跳转到微信
        
        if (@available(iOS 8.0, *)) {
            if ([[UIApplication sharedApplication] canOpenURL:components.URL]) {
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL:components.URL options:@{} completionHandler:NULL];
                } else {
                    [[UIApplication sharedApplication] openURL:components.URL];
                }
            }
        } else {
            if ([[UIApplication sharedApplication] canOpenURL:components.URL]) {
                [[UIApplication sharedApplication] openURL:components.URL];
            }
        }
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

@end
